package top.sehnsucht.dao.impl;

import top.sehnsucht.dao.StudentDao;
import top.sehnsucht.entity.Student;
import top.sehnsucht.entity.User;
import top.sehnsucht.util.DBUtil;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Description:
 * @Author: Cai
 * @CreateTime: 2021/12/21
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public ArrayList<Student> selectAllStudent() {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        ArrayList<Student> students = new ArrayList<>();
        try {
            conn = DBUtil.getConn();
            String sql = "select id,name,school from student";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                Integer id = rs.getInt(1);
                String name = rs.getString(2);
                String school = rs.getString(3);
                Student student = new Student(id,name,school);
                students.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return students;
    }
}
